package com.zhongkun.datahome.service.res;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Collections;
import java.util.List;

/**
 * Created by likui at 2021/9/9 2:08 PM
 */
@Data
public class PageVo<T>{
    @ApiModelProperty("结果集")
    private List<T> list;

    @ApiModelProperty("当前第几页")
    private int pageNo;

    @ApiModelProperty("每页数目")
    private int pageSize;

    @ApiModelProperty("总记录数")
    private long totalRecords;

    @ApiModelProperty("总页数")
    private long totalPages;

    public PageVo(List<T> list, int pageNo, int pageSize, long totalRecords) {
        this.list = list;
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalRecords = totalRecords;
        this.totalPages = totalRecords % pageSize > 0 ? totalRecords / pageSize + 1 : totalRecords / pageSize;
    }


    public PageVo(IPage<T> page) {
        this.setList(page.getRecords());
        this.setTotalRecords(page.getTotal());
        this.setPageNo((int) page.getCurrent());
        this.setPageSize((int) page.getSize());
        this.totalPages = totalRecords % pageSize > 0 ? totalRecords / pageSize + 1 : totalRecords / pageSize;
    }

    public PageVo() {

    }

    public static <T> PageVo<T> of(IPage<T> page) {
        return new PageVo<>(page);
    }

    public static <T> PageVo<T> emptyPage(int pageNo, int pageSize) {
        PageVo<T> pageResult = new PageVo<>();
        pageResult.setPageNo(pageNo);
        pageResult.setPageSize(pageSize);
        pageResult.setTotalPages(0);
        pageResult.setTotalRecords(0);
        pageResult.setList(Collections.emptyList());

        return pageResult;
    }
}
